<?php
class AdminController extends Zend_Controller_Action {
	public function init() {
		$ajaxContext = $this->_helper->getHelper('AjaxContext');
		$ajaxContext->addActionContext('disable','json');
		$ajaxContext->addActionContext('enable','json');
		$ajaxContext->addActionContext('changerule','json');
		$ajaxContext->initContext();
	}
	
	/***
	 * 
	 */
	public function indexAction() {
		
	}
	/***
	 * 
	 */
	public function userAction() {
		$users = Core_Business_User::getAllUser();
		$roles = Core_Business_Rule::getRules();
		$listroles=array();
		foreach ($roles as $role) {
			$listroles[$role->Id]=$role->Name;
		}
		
		$UserCollection = array();
		if(isset($users)) {
			foreach ($users as $item) {
				if($item->Id != $this->_helper->Acl->getUserId())
					$UserCollection[] = array("id"=>$item->Id,"Nom"=>$item->Lastname,"Prenom"=>$item->Firstname,"Email"=>$item->Email,"Role"=>$item->getRoleId(),"disable"=>$item->getDisabled(),"listroles"=>$listroles);
			}
		}
		$this->view->users =$UserCollection; 
		$this->view->listroles=$listroles;
	}
	public function ruleAction(){
		$rules = Core_Business_Rule::getRules();
		$privilege = Core_Authentication_Business_Resourceprivilege::getAcls();
		$table = array();
		foreach($privilege as $item) {
			$table[] = array("label"=>$item->getResource()->Name."<>".$item->getPrivilege()->Name,"id"=>$item->getResourceId()."_".$item->getPrivilegeID()."_".$item->getRuleID(),"value"=>$item->Allow); 
		}
		$this->view->table = $table;
	}
	/***
	 * 
	 */
	public function statAction() {}
	
	/**
	 * Action used for disabled a user
	 */
	public function disableAction() {
		if($this->getRequest()->isGet()) {
			$userid = $this->getRequest()->getParam('id');
			if(isset($userid)) {
				$user = Core_Business_User::getById($userid);
				$user->Disabled=1;
				$user->save();
				$this->view->output = Zend_Json::encode("true");
			}	
		}
	}

	/**
	 * Action used for enabled a user
	 */
	public function enableAction() {
		if($this->getRequest()->isGet()) {
			$userid = $this->getRequest()->getParam('id');
			if(isset($userid)) {
				$user = Core_Business_User::getById($userid);
				$user->Disabled=0;
				$user->save();
				$this->view->output = Zend_Json::encode("true");
			}	
		}
	}
	public function changeruleAction() {
		if($this->getRequest()->isGet()) {
			$userid = $this->getRequest()->getParam('id');
			$ruleid = $this->getRequest()->getParam('rule');
			if(isset($userid) && isset($ruleid)) {
				$user = Core_Business_User::getById($userid);
				$ruleuser = new Core_Business_Userrule();
				$ruleuser->Userid = $userid;
				$ruleuser->Ruleid = $ruleid;
				$ruleuser->save();
				$this->view->output = Zend_Json::encode("true");
			}	
		}
	}
}
?>